package com.nexgo.payment.pack;

import com.nexgo.data.entity.SendData;
import com.nexgo.data.entity.SendDataBuilder;
import com.nexgo.data.json.JsonSendData;
import com.nexgo.data.json.JsonTools;
import com.nexgo.data.json.bean.JsonBody;
import com.nexgo.data.json.bean.SendBodyData;
import com.nexgo.data.json.bean.SendHead;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.databases.DataSource;
import com.nexgo.payment.entity.MessageData;
import com.nexgo.payment.entity.TransData;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 重置GPS信息
 * Created by lr on 2017/9/1.
 */
class ResetGpsBuilder extends DataBuilder{
    private Logger log;

    ResetGpsBuilder(TransData transData, DataSource repository) {
        super(transData, repository);
        log = LoggerFactory.getLogger(this.getClass().getName());
    }

    @Override
    public SendData form() {
        return null;
    }

    @Override
    public MessageData formJsonData() {
        MessageData sSendData = new MessageData();
        JsonSendData jsonSendData = new JsonSendData();
        SendHead head = new SendHead();
        JsonBody jsonBody = new JsonBody();

        //head
        head.setTRAN_PROCESS("");
        head.setTRAN_ID("");
        head.setTranscode("CIPP050004");
        head.setSign("0000000000000000");
        String date = new SimpleDateFormat("yyyyMMddHHmmss", SysConstant.LOCALE).format(new Date());
        head.setTerm_trans_time(date);
        head.setMcht_id(mRepository.getParamValue(PreferencesConstants.MERCHANT_ID));
        //经纬度，签名暂时为空
        head.setLngx(mRepository.getParamValue(PreferencesConstants.LONGITUDE));
        head.setLngy(mRepository.getParamValue(PreferencesConstants.LATITUDE));
        jsonSendData.setREQ_HEAD(head);


        SendData sendData  = new SendDataBuilder().setListener(mListener).setSignListener(mSignListener).createSendData();
        sSendData.setSendData(sendData);

        //body
        jsonBody.setTerm_id(mRepository.getParamValue(PreferencesConstants.TERMINAL_ID));
        String resetgpsrandom = mRepository.getParamValue(PreferencesConstants.RESETGPSRANDOM);
        String resetgpsckv = mRepository.getParamValue(PreferencesConstants.RESETGPSCKV);
        log.debug("ResetGpsBuilder formJsonData RESETGPSCKV = {},RESETGPSRANDOM = {}",resetgpsckv,resetgpsrandom);
        jsonBody.setNonce_str(resetgpsrandom);
        jsonBody.setCheck_val(resetgpsckv);
        SendBodyData sendBodyData = new SendBodyData();
        String str = JsonTools.toJson(jsonBody);
        sendBodyData.setDECRYPT(str);

        jsonSendData.setREQ_BODY(sendBodyData);

        sSendData.setJsonSendData(jsonSendData);

        setScan(true);

        return sSendData;
    }

}
